@VeryStart:
EnableEvents()
GoToIfCoordsIn(@StartInBank,87,689,93,700)
GoToIfCoordsIn(@StartInSmith,86,681,82,678)

;Start Outside
Debug("===Starting outside")
MoveTo(91,686)
MoveTo(88,686,-2,2,-2,9,2,9,4,11)


GoTo(@VeryStart)



@StartInSmith:
Debug("===Starting in smith")
MoveTo(82,679)
MoveTo(81,679,0,3)
GoTo(@VeryStart)



@StartInBank:
Debug("===Starting in bank")

EnableEvents()

WatchObjects(1)
OnRetryDest(@ABitStuck)
OnCoordsChanged(@CoordsChanged,%LX,%LY)


SetSkipDestTime(155)
SetRetryDestTime(10)
SetVarNum(%runs,0)

@banking:
AddVarNum(%runs,1)
GoToIfVarAboveNum(@GetSomeSleep,%runs,25)
SetVarFatigue(%v)
DebugVar(%v)
GoToIfVarAboveNum(@GetSomeSleep,%v,650)

;Debug("=========BANK=====")

OnTimer(@VeryStart,1,1200)

@BankingTalk:
ResetQuestMenu()
ResetLastServerMessage()
SetVarsRandomNPCIn(%x,%y,%ID,87,689,93,700,268)
ActionVarVar(%x,%y)
TalkToNPCVar(%ID)
WaitForQuestMenu(15)
GoToIfLastServerMessageIs(@BankingTalk,"Banker is busy at the moment")
GoToIfQuestMenu(@ContactWithBanker)
WaitForQuestMenu(15)
GoToIfLastServerMessageIs(@BankingTalk,"Banker is busy at the moment")
GoToIfQuestMenu(@ContactWithBanker)
WaitForQuestMenu(15)
GoToIfLastServerMessageIs(@BankingTalk,"Banker is busy at the moment")
GoToIfQuestMenu(@ContactWithBanker)
GoTo(@BankingTalk)

@ContactWithBanker:
Wait(10)
Answer(0)
WaitForBankWindow(50)
Wait(5)
DepositEverything()
Wait(10)
ResetLastServerMessage()
@WithdrawStuff:
Withdraw(151,1)
Withdraw(155,1)
Withdraw(155,1)
Wait(2)
GoToIfBagFull(@FullHands)
GoToIfLastServerMessageIsNot(@WithdrawStuff,"You don't have room to hold that object!")
@FullHands:
Wait(10)
CloseBank()
Wait(10)
GoToIfInInventory(@banking,171)
;- go back mining

OnTimer(@VeryStart,1,1200)

MoveToNoWait(87,695)
Wait(20)
MoveTo(86,695,0,-1,-2,-3)
MoveTo(85,687,-1,-1)
MoveTo(84,685,-3,-3,-3,-6)
MoveToNoWait(84,679)
Wait(20)

;- smithing
@SmeltStuff:
Action(84,679)
AtObjectWithItem(85,679,3)
Wait(10)
WaitForServerMessage(20)
GoToIfInInventory(@HaveIron,151)
GoTo(@BackToBank)
@HaveIron:
GoToIfCountInInventoryGreater(@SmeltStuff,1,155)



@BackToBank:
;- back to bank
MoveTo(80,679)
MoveTo(80,682,2,2,2,9,5,12)
MoveTo(86,695,3,0)


GoTo(@banking)



@GetSomeSleep:
OnTimer(@VeryStart,1,4200)
SetVarNum(%runs,0)
UseItem(0)
ResetLastServerMessage()
SetVarNum(%secs,0)
@JustWait:
Wait(10)
AddVarNum(%secs,1)
GoToIfVarAboveNum(@GetSomeSleep,%secs,70)
GoToIfLastServerMessageIsNot(@JustWait,"You wake up - feeling refreshed")
SetVarNum(%times,0)
GoTo(@banking)




;===Event OnRetryDest ABitStuck
; Check where exactly we're stuck
; If it's at doors, then try to
; open them, wait a bit and move
@ABitStuck:
GoToIfCoordsIn(@StuckOutsideBank,86,695,86,696)
GoToIfCoordsIn(@StuckInsideBank,87,695,87,696)
--Debug("stuck")
ERet()


;===Procedure StuckOutsideBank
; Get's called if bank is closed
@StuckOutsideBank:
SetTimeSinceMovingToDest(0)
Debug("Stuck outside bank, trying to open door")
ERetToRet()
Action(86,695)
AtObject(86,695)
Wait(20)
SetTimeSinceMovingToDest(0)
MoveTo(90,695)
Ret()


;===Procedure StuckInsideBank
; Get's called if bank is closed
@StuckInsideBank:
SetTimeSinceMovingToDest(0)
Debug("Stuck inside bank, trying to open door")
ERetToRet()
Action(87,695)
AtObject(86,695)
Wait(20)
SetTimeSinceMovingToDest(0)
MoveTo(86,695,-1,-1)
Ret()




;====Event OcCoordsChanged
@CoordsChanged:
GoToIfCoordsIn(@GoodCoords,75,675,93,700)
Debug("Oooops, we are not at smelting site. Teleported?")
SaveScreenshot()
Wait(20)
StopAndLogOut()

@GoodCoords:
ERet()

